//
//  SAppUtil.swift
//  niuguwang
//
//  Created by BrightLi on 2021/2/13.
//  Copyright © 2021年 taojinzhe. All rights reserved.
//

import SystemConfiguration.CaptiveNetwork
import UIKit
import AdSupport
import AVKit
import Photos

@objcMembers open class SAppUtil:NSObject
{
    public static var shared = SAppUtil()
    
    lazy var platformList:Dictionary<String,String> = {
        let result = ["iPhone1,1":"iPhone 1G",
                      "iPhone1,2":"iPhone 3G",
                      "iPhone2,1":"iPhone 3GS",
                      "iPhone3,1":"iPhone 4",
                      "iPhone3,2":"iPhone 4",
                      "iPhone3,3":"iPhone 4",
                      "iPhone4,1":"iPhone 4S",
                      "iPhone5,1":"iPhone 5",
                      "iPhone5,2":"iPhone 5",
                      "iPhone5,3":"iPhone 5C",
                      "iPhone5,4":"iPhone 5C",
                      "iPhone6,1":"iPhone 5S",
                      "iPhone6,2":"iPhone 5S",
                      "iPhone7,1":"iPhone 6 Plus",
                      "iPhone7,2":"iPhone 6",
                      "iPhone8,1":"iPhone 6s",
                      "iPhone8,2":"iPhone 6s Plus",
                      "iPhone8,4":"iPhone SE",
                      "iPhone9,4":"iPhone 7 Plus",
                      "iPhone9,2":"iPhone 7 Plus",
                      "iPhone9,3":"iPhone 7",
                      "iPhone9,1":"iPhone 7",
                      "iPhone10,1":"iPhone 8",
                      "iPhone10,4":"iPhone 8",
                      "iPhone10,2":"iPhone 8 Plus",
                      "iPhone10,5":"iPhone 8 Plus",
                      "iPhone10,3":"iPhone X",
                      "iPhone10,6":"iPhone X",
                      "iPhone11,2":"iPhone XS",
                      "iPhone11,4":"iPhone XS Max",
                      "iPhone11,6":"iPhone XS Max",
                      "iPhone11,8":"iPhone XR",
                      
                      "iPhone12,1":"iPhone 11",
                      "iPhone12,3":"iPhone 11 Pro",
                      "iPhone12,5":"iPhone 11 Pro Max",
                      "iPhone12,8":"iPhone SE (2nd generation)",
                      "iPhone13,1":"iPhone 12 mini",
                      "iPhone13,2":"iPhone 12",
                      "iPhone13,3":"iPhone 12 Pro",
                      "iPhone13,4":"iPhone 12 Pro Max",
                      
                      
                      "iPod1,1":"iPod Touch 1G",
                      "iPod2,1":"iPod Touch 2G",
                      "iPod3,1":"iPod Touch 3G",
                      "iPod4,1":"iPod Touch 4G",
                      "iPod5,1":"iPod Touch 5G",
                      "iPod7,1":"iPod Touch 6G",
                      "iPad1,1":"iPad",
                      "iPad1,2":"iPad 3G",
                      "iPad2,1":"iPad 2(WiFi)",
                      "iPad2,2":"iPad 2",
                      "iPad2,3":"iPad 2(CDMA)",
                      "iPad2,4":"iPad 2",
                      "iPad2,5":"iPad Mini(WiFi)",
                      "iPad2,6":"iPad Mini",
                      "iPad2,7":"iPad Mini(GSM+CDMA)",
                      "iPad3,1":"iPad 3(WiFi)",
                      "iPad3,2":"iPad 3",
                      "iPad3,3":"iPad 3",
                      "iPad3,4":"iPad 4(WiFi)",
                      "iPad3,5":"iPad 4",
                      "iPad3,6":"iPad 4(GSM+CDMA)",
                      "iPad4,1":"iPad Air(WiFi)",
                      "iPad4,2":"iPad Air(Cellular)",
                      "iPad4,4":"iPad Mini 2(WiFi)",
                      "iPad4,5":"iPad Mini 2(Cellular)",
                      "iPad4,6":"iPad Mini 2",
                      "iPad4,7":"iPad Mini 3",
                      "iPad4,8":"iPad Mini 3",
                      "iPad4,9":"iPad Mini 3",
                      "iPad5,1":"iPad Mini 4(WiFi)",
                      "iPad5,2":"iPad Mini 4(LTE)",
                      "iPad5,3":"iPad Air 2",
                      "iPad5,4":"iPad Air 2",
                      "iPad6,3":"iPad Pro 9.7",
                      "iPad6,4":"iPad Pro 9.7",
                      "iPad6,7":"iPad Pro 12.9",
                      "iPad6,8":"iPad Pro 12.9",
                      "iPad6,11":"iPad 5(WiFi)",
                      "iPad6,12":"iPad 5(Cellular)",
                      "iPad7,1":"iPad Pro 12.9(WiFi)",
                      "iPad7,2":"iPad Pro 12.9(Cellular)",
                      "iPad7,3":"iPad Pro 10.5(WiFi)",
                      "iPad7,4":"iPad Pro 10.5(Cellular)",
                      "iPad7,5":"iPad New 9.7",
                      
                      "i386":"Simulator",
                      "x86_64":"Simulator"
        ]
        return result
    }()
    // 设备型号
    dynamic public class var platform:String{
        get{
            return shared.platform()
        }
    }
    dynamic public class var systemName:String{
        get{
            let result=UIDevice.current.systemName
            return result
        }
    }
    dynamic public class var systemVersion:String{
        get{
            let result=UIDevice.current.systemVersion
            return result
        }
    }
    // 版本号
    dynamic public class var shortVersion:String{
        get{
            let shortVersion=Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
            return shortVersion
        }
    }
    // 编译版本
    dynamic public class var build:String{
        get{
            let build=Bundle.main.infoDictionary!["CFBundleVersion"] as! String
            return build
        }
    }
    // 包名
    dynamic public class var bundleId:String{
        get{
            let bundleId=Bundle.main.infoDictionary!["CFBundleIdentifier"] as! String
            return bundleId
        }
    }
    // App短名
    dynamic public class var shortAppName:String{
        get{
            if let temp = appName.components(separatedBy: "-").first{
                return temp
            }
            return appName
        }
    }
    // App名字
    dynamic public class var appName:String{
        get{
            let appName=Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String
            return appName
        }
    }
    // 更换App图标
    dynamic public class func changeAppIcon(_ iconName:String)
    {
        if #available(iOS 10.3, *) {
            if UIApplication.shared.supportsAlternateIcons {
                if let _ = UIApplication.shared.alternateIconName{
                }else{
                    UIApplication.shared.setAlternateIconName(iconName, completionHandler: {(_ error: Error?) -> Void in
                        if error != nil {
                            SLog.debug("设置应用图标错误:%",String(describing: error))
                        }else{
                            SLog.debug("设备应用图标成功")
                        }
                    })
                }
            }
        }
    }
    // 还原App图标
    dynamic public class func restoreAppIcon()
    {
        if #available(iOS 10.3, *) {
            if UIApplication.shared.supportsAlternateIcons {
                if let icon = UIApplication.shared.alternateIconName{
                    SLog.debug("当前应用图标:%",icon)
                    UIApplication.shared.setAlternateIconName(nil, completionHandler: {(_ error: Error?) -> Void in
                        if error != nil {
                            SLog.debug("还原应用图标错误:%",String(describing: error))
                        }else{
                            SLog.debug("还原应用图标成功")
                        }
                    })
                }
            }
        }
    }
    // 获得IDFA
    dynamic public class func idfa()->String
    {
        if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
            let idfa=ASIdentifierManager.shared().advertisingIdentifier.uuidString
            YMKeyChain.setString(idfa, forKey: "idfa")
            return idfa
        }
        var idfa=YMKeyChain.string(forKey: "idfa")
        if idfa == nil{
            idfa="00000000-0000-0000-0000-000000000000"
        }
        return idfa!
    }
    // 是否为模拟器
    dynamic public class func isSimulate()->Bool{
        return Platform.isSimulator
    }
    // 模块名称
    dynamic public class var modelName:String
        {
        get{
            let className=NSStringFromClass(SAppUtil.self)
            let temp=className.split(separator: ".")
            if temp.count>1{
                let modelName=String(temp[0])
                return modelName
            }
            return ""
        }
    }
    
    dynamic func platform()->String{
        if Platform.isSimulator{
            return "模拟器"
        }
        let platform = SOCExtend.platform() as String
        let simple = SDataUtil.stringOf(platformList[platform],valid:platform)
        return simple
    }
    
    
    dynamic public class func canPhotoAlbum(){
        let authStatus = PHPhotoLibrary.authorizationStatus()
        if authStatus == .notDetermined{
            PHPhotoLibrary.requestAuthorization{status in
                if status == .authorized{
                    SLog.debug("允许访问相册权限")
                }else{
                    SLog.debug("拒绝访问相册权限")
                }
            }
        }else if authStatus == .authorized{
        }else{
            let alert = YSAlert()
            alert.content="请在iPhone的\"设置-隐私-照片\"中允许牛股王访问你的手机照片"
            alert.sureTitle = YMBridging.kLanguage("i_know_txt")
            alert.show()
        }
    }
    // 相机权限
    dynamic public class func cameraPermissions(authorizedBlock: ChangeBlock?, deniedBlock: ChangeBlock?) {
        let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
        if authStatus == .notDetermined {
            AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) in
                DispatchQueue.safeMainAsync{
                    cameraPermissions(authorizedBlock: authorizedBlock, deniedBlock: deniedBlock)
                }
            })
        } else if authStatus == .authorized {
            authorizedBlock?()
        } else {
            deniedBlock?()
        }
    }
    // 相册权限
    dynamic public class func photoAlbumPermissions(authorizedBlock: ChangeBlock?, deniedBlock: ChangeBlock?) {
        let authStatus = PHPhotoLibrary.authorizationStatus()
        if authStatus == .notDetermined {
            PHPhotoLibrary.requestAuthorization { (status:PHAuthorizationStatus) -> Void in
                DispatchQueue.safeMainAsync{
                    photoAlbumPermissions(authorizedBlock: authorizedBlock, deniedBlock: deniedBlock)
                }
            }
        } else if authStatus == .authorized{
            authorizedBlock?()
        } else {
            deniedBlock?()
        }
    }
    
    
}
